home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / jaz_clib.arc / LITERAL.C < prev    next >
Text File  |  1989-04-09  |  4KB  |  130 lines

  1.  
  2. /*
  3. ┌────────────────────────────────────────────────────────────────────────────┐
  4. │literal.c                                     │
  5. │Translate a string with tokens in it according to several conventions:      │
  6. │ 1.      delineate comments                             │
  7. │ 2. \x1b translates to char(0x1b)                         │
  8. │ 3. \27  translates to char(27)                         │
  9. │ 4. \"   translates to "                                                    │
  10. │ 5. \'   translates to '                                                    │
  11. │ 6. \\   translates to \                             │
  12. │                                         │
  13. │ Synopsis                                     │
  14. │    *s = "this\x20is\32a test of \\MSC\\JAZ\7"                              │
  15. │    literal(s,d);                                 │
  16. │                                         │
  17. │    ( d now equals "this is a test of \MSC\JAZ")                           │
  18. └────────────────────────────────────────────────────────────────────────────┘
  19. */
  20.  
  21. #include <ctype.h>
  22.  
  23. #define HEX "0123456789ABCDEF"
  24.  
  25. literal(fsource,fdestin)
  26. char *fsource;        /* original string */
  27. char *fdestin;        /* returned string */
  28. {
  29.  
  30.   int wpos,w;
  31.   int x;
  32.   int winquote;
  33.   int wincomment;
  34.   unsigned char wchar;
  35.  
  36.   winquote = 0;
  37.   wincomment = 0;
  38.  
  39.   *fdestin = 0;         /* start out with NULL string */
  40.   w = 0;            /* set index to first character */
  41.   while (fsource[w]) {        /* until end of string */
  42.     switch(fsource[w]) {
  43.       case '"' :                /* check quotes */
  44.       case '\\':
  45.     if ( ! wincomment )
  46.       switch(fsource[w+1]) {
  47.         case 'x' :                    /* Hexidecimal */
  48.           wchar = 0;
  49.           w++;              /* get past "\" */
  50.           w++;              /* get past "x" */
  51.           if (index(HEX,toupper(fsource[w])) != -1)
  52.         while ((wpos = index(HEX,toupper(fsource[w]))) != -1) {
  53.           wchar = (wchar << 4) + wpos;
  54.           w++;
  55.         }
  56.           else            /* just an x */
  57.         wchar = 'x';
  58.           w --;
  59.           *fdestin ++ = wchar;
  60.           break;
  61.         case '\\' :                 /* we want a "\" */
  62.           w ++;
  63.           *fdestin++ = '\\';
  64.           break;
  65.         case 't' :                  /* tab char */
  66.           w ++;
  67.           *fdestin++ = 9;
  68.           break;
  69.         case 'n' :                  /* new line */
  70.           w ++;
  71.           *fdestin++ = 13;
  72.           *fdestin++ = 10;
  73.           break;
  74.         case 'r' :                  /* carr return */
  75.           w ++;
  76.           *fdestin++ = 13;
  77.           break;
  78.         case 'b' :                  /* back space */
  79.           w ++;
  80.           *fdestin++ = 8;
  81.           break;
  82.         case '\'' :                 /* single quote */
  83.           w ++;
  84.           *fdestin++ = '\'';
  85.           break;
  86.         case '\"' :                 /* double quote */
  87.           w ++;
  88.           *fdestin++ = '\"';
  89.           break;
  90.         default :              /* DECIMAL */
  91.           w ++;              /* get past "\" */
  92.           wchar = 0;
  93.           if (index("0123456789",fsource[w]) != -1) {
  94.         do
  95.           wchar = wchar * 10 + (fsource[w++] - 48); /* cnv to binary */
  96.         while (index("0123456789",fsource[w]) != -1);
  97.         w --;
  98.           }
  99.           else
  100.         wchar = fsource[w];
  101.           *fdestin ++ = wchar;
  102.           break;
  103.       }
  104.       case '\'':
  105.     winquote ^= 1;        /* toggle flag */
  106.     break;
  107.       case '*' :
  108.     if (wincomment)
  109.       if (fsource[w+1] == '/') {
  110.           wincomment = 0 ;              /* toggle the flag */
  111.           w ++;
  112.           break;
  113.       }
  114.       case '/' :              /* beginning of comment perhaps */
  115.     if (fsource[w+1] == '*') {
  116.         wincomment = 1 ;            /* toggle the flag */
  117.         w ++;
  118.         break;
  119.     }
  120.       default :
  121.     if ( ! wincomment) {
  122.       *fdestin++ = fsource[w];
  123.       break;
  124.     }
  125.     }
  126.     w ++;
  127.   }
  128.   *fdestin = 0;         /* terminate the string */
  129. }
  130.